package com.thinkfit.jy.activity;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.thinkfit.jy.R;
import com.thinkfit.jy.fragment.VideoListFragment;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.MotionDataUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.ThemeUtils;
import com.thinkfit.jy.utils.Utils;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 视频课程
 */
public class VideoActivity extends MainActivity {

    private ArrayList<BaseFragment> mFragments;
    private int mCurrentItem;
    private int mCurrentStyle;
    RelativeLayout rlMain;
    TextView tvArea,tvLanguage,tvUnit;

    @Override
    protected int getLayoutId() {
//        mCurrentStyle = DataManager.Companion.getInstance().getMainStyle();
//        return mCurrentStyle == 3 ? R.layout.activity_video_xm :
          return R.layout.activity_video;
    }

    public void init() {
        resetDate();
        setViewsOnClickListener(R.id.ll_sport_back,R.id.ivHome, R.id.ivSound, R.id.ivAvatar, R.id.ivSetting,R.id.ivBluetooth,R.id.llStart);
        setTextTypeFace(R.id.tvSlope, R.id.tvTime, R.id.tvDistance, R.id.tvSpeed);
        rlMain = findViewById(R.id.rl);
        tvArea = findViewById(R.id.tv_area);
        tvLanguage = findViewById(R.id.tv_language);
        tvUnit = findViewById(R.id.tv_unit);
        if (MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.ROWING_MACHINE)) {
            setText(R.id.tvSlopTitle, R.string.app_resistance);
            setText(R.id.tvSpeedTitle, R.string.app_pace_500m);
        }
        resetDate();
        if(DeviceTypeUtils.connectDeviceType() == 0){
            showStartBtn(MotionDataUtils.getInstance().isRun());

        }else {
            showStartBtn(MotionDataUtils.getInstance().isRun());
            resetInfo(MotionDataUtils.getInstance().getCurrentInfo());
        }

        setViewsOnClickListener(R.id.tv0, R.id.tv1, R.id.tv2, R.id.tv3);
        int type = getIntent().getExtras().getInt(Constants.BUNDLE_EXTRA);
//        int type = DeviceTypeUtils.getDeviceType();
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa    DeviceType : "+type);
        mCurrentItem = 0;
        mFragments = new ArrayList<>();
        mFragments.add(VideoListFragment.getInstance(0, type));
        mFragments.add(VideoListFragment.getInstance(1, type));
        mFragments.add(VideoListFragment.getInstance(2, type));
        mFragments.add(VideoListFragment.getInstance(3, type));
        ViewPager viewPager = findViewById(R.id.viewPager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurrentItem = position;
                resetBtn();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(mCurrentItem);
        Utils.initRegion(tvArea,tvLanguage,tvUnit);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv0:
                ViewPager viewPager = findViewById(R.id.viewPager);
                viewPager.setCurrentItem(0);
                break;
            case R.id.tv1:
                viewPager = findViewById(R.id.viewPager);
                viewPager.setCurrentItem(1);
                break;
            case R.id.tv2:
                viewPager = findViewById(R.id.viewPager);
                viewPager.setCurrentItem(2);
                break;
            case R.id.tv3:
                viewPager = findViewById(R.id.viewPager);
                viewPager.setCurrentItem(3);
                break;
            case R.id.ll_sport_back:
                finish();
                break;
            default:
                super.onClick(v);
                break;
        }
    }

    public void onResume() {
        super.onResume();
        NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
        SportsStatusBarViewUtils.getInstance().isMain(false);
        rlMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        if (map.containsKey(EventBusEvent.TIME_CHANGE)) {
            resetDate();
        }else {
            super.onReceive(map);
        }
    }

    private void resetBtn() {
        for (int i = 0; i < 4; ++i) {
            int id = getResources().getIdentifier("tv" + i, "id", getPackageName());
            TextView tv = findViewById(id);
            if (i == mCurrentItem) {
                if (mCurrentStyle == 3) {
                    tv.setBackgroundResource(0);
                } else {
                    tv.setBackgroundResource(R.drawable.shape_gradient_18_d0b085_a87c42_360);
                    tv.setTextColor(ContextCompat.getColor(this, com.common.lib.R.color.white));
                }
            } else {
                tv.setBackground(null);
                if (mCurrentStyle != 3) {
                    tv.setTextColor(ContextCompat.getColor(this, com.common.lib.R.color.text_color_1));
                }
            }
        }
    }

    @Override
    public boolean isMainActivity() {
        return false;
    }

}